﻿using Architecture.Api.Application.Commands.RoleCommand;
using Architecture.Domain.Services.Impl;
using Architecture.Domain.Services.Interface;
using Architecture.Domian;
using Architecture.Domian.Common;
using Architecture.Domian.DTO;
using Architecture.Domian.Entity;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Architecture.Api.Controllers
{
    /// <summary>
    /// 角色管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class RoleController : ControllerBase
    {
        private readonly IRoleSerivce _roleService;
        private readonly IMediator _mediator;

        public RoleController(IRoleSerivce roleSerivce, IMediator mediator)
        {
            _roleService = roleSerivce;
            _mediator = mediator;
        }

        /// <summary>
        /// 角色添加
        /// </summary>
        /// <param name="command">角色实体</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public async Task<int> Create(RoleCreateCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 角色多条件查询分页对象列表
        /// </summary>
        /// <param name="command">当前页容量，当前页索引</param>
        /// <returns>角色多条件查询分页对象列表</returns>
        [HttpGet]
        public async Task<PageResult<RoleInfo>> GetRoleList([FromQuery] RoleReadCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 更新角色信息
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> RoleUpdateNew(RoleUpdateCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 逻辑删除角色信息
        /// </summary>
        /// <param name="command">角色Id</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public async Task<int> LogicDelete([FromQuery] RoleLogicDeleteCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据角色Id获取角色信息
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns>角色信息</returns>
        [HttpGet]
        public async Task<RoleInfo> GetRoleInfoByRoleId([FromQuery] RoleReadByIdCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据部门Id获取该部门下的用户列表
        /// </summary>
        /// <param name="departmentId">部门Id</param>
        /// <returns>用户列表</returns>
        [HttpGet]
        public async Task<List<TreeList>> GetUserTreeListByDepartmentId(int departmentId)
        {
            var res = _roleService.GetUserTreeListByDepartmentId(departmentId);
            return await res;
        }

        /// <summary>
        /// 批量添加用户角色关系
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="userIds">用户Id集合</param>
        /// <returns>受影响行数</returns>
        [HttpPost]
        public async Task<int> UserRoleBatchInsert(RoleBatchCreateCommand command)
        {
            return await _mediator.Send(command);
        }

        /// <summary>
        /// 根据角色Id获取用户列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns>该角色下的用户列表</returns>
        [HttpGet]
        public async Task<List<UserRoleDTO>> GetUserListByRoleId(int roleId)
        {
            var res = _roleService.GetUserListByRoleId(roleId);
            return await res;
        }


        /// <summary>
        /// 根据角色Id和用户Id删除用户角色信息
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <param name="userId">用户Id</param>
        /// <returns>受影响行数</returns>
        [HttpDelete]
        public async Task<int> DeleteUserRoleInfo(int roleId, int userId)
        {
            var res = _roleService.DeleteUserRoleInfo(roleId, userId);
            return await res;
        }

    }
}
